<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8"/>
    <title>首页 - OAuth2登录示例</title>
    <style>
        .container {
            max-width: 800px;
            margin: 50px auto;
            padding: 20px;
            border: 1px solid #ddd;
            border-radius: 5px;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
        }
        .login-tip {
            color: #dc3545;
        }
        .user-info {
            margin-bottom: 20px;
        }
        .user-info img {
            width: 100px;
            height: 100px;
            border-radius: 50%;
            margin-right: 20px;
        }
        .user-details {
            display: inline-block;
            vertical-align: top;
        }
        .user-details p {
            margin: 5px 0;
        }
        .logout-btn {
            padding: 10px 20px;
            background-color: #dc3545;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            text-decoration: none;
            display: inline-block;
        }
        .logout-btn:hover {
            background-color: #c82333;
        }
        .login-tip {
            text-align: center;
            margin-top: 20px;
        }
        .login-link {
            color: #007bff;
            text-decoration: none;
        }
        .login-link:hover {
            text-decoration: underline;
        }
    </style>
</head>
<body>
<div class="container">
    <div id="userInfoContainer">
        <!-- 用户信息将通过JavaScript动态加载 -->
    </div>
</div>

<script>
    // 获取用户信息
    fetch('/auth/user/current')
        .then(response => {
            if (!response.ok) {
                throw new Error('未登录');
            }
            return response.json();
        })
        .then(user => {
            const container = document.getElementById('userInfoContainer');
            container.innerHTML = `
                <div  class="login-tip">登录成功</div>
                <div class="user-info">
                    <img src="${user.avatar || '/auth/images/default-avatar.png'}" alt="用户头像">
                    <div class="user-details">
                        <h2>${user.nickname || user.username}</h2>
                        <p><strong>用户名：</strong>${user.username}</p>
                        <p><strong>邮箱：</strong>${user.email || '未设置'}</p>
                        <p><strong>登录方式：</strong>${user.provider}</p>
                        <p><strong>登录时间：</strong>${new Date().toLocaleString()}</p>
                    </div>
                </div>
                <a href="/auth/logout" class="logout-btn">退出登录</a>
            `;
        })
        .catch(error => {
            const container = document.getElementById('userInfoContainer');
            container.innerHTML = `
                <div class="login-tip">
                    <p>您尚未登录，请先登录</p>
                    <a href="/auth/login.html" class="login-link">去登录</a>
                </div>
            `;
        });
</script>
</body>
</html>